<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width" />
    <title>Page 2</title>
    <style>
      body {
        background-color: aliceblue;
        font-family: monospace;
      }
    </style>
  </head>
  <body>
    <p class="output">page2.html (iframe body)</p>
    <script>
      const output = document.querySelector(".output");

      window.addEventListener("message", onMessage);

      function onMessage(e) {
        if (!e.ports.length) return;

        output.innerText = e.data;
        // Use the transferred port to post a message to the main frame
        e.ports[0].postMessage("A message from the iframe in page2.html");
      }
    </script>
  </body>
</html>
